package com.ziyun.pop.mall.module.mall.service;

import com.service.common.model.PageParamDto;
import com.service.goods.api.CategoryService;
import com.service.goods.api.SellerService;
import com.service.goods.constant.GoodsResult;
import com.service.goods.model.category.CategoryBaseResultDto;
import com.service.goods.model.category.CategoryResultDto;
import com.service.goods.model.category.GoodsVO;
import com.service.goods.model.search.GoodsCacheSearchResultDto;
import com.service.goods.model.search.GoodsSearchParamDto;
import com.service.goods.model.seller.SellerInfoVO;
import com.service.goods.model.seller.SellerListParamDto;
import com.service.goods.model.seller.SellerListResultDto;
import com.service.quote.module.product.QuoteProductService;
import com.service.quote.module.product.model.QuoteProductInfo;
import com.utility.New;
import com.ziyun.pop.mall.module.mall.result.ProductResult;
import com.ziyun.pop.mall.module.mall.result.SellerCategoryInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @Author:chenxiaoyi
 * @Date:2018/8/31 8:53
 */

@Service
@Transactional
public class MallIndexService {

    private static Logger _log = LoggerFactory.getLogger(MallIndexService.class);

    @Autowired
    private CategoryService categoryService;


    @Autowired
    private QuoteProductService quoteProductService;
    
    @Autowired
    private SellerService sellerService;


    //获取首页供应商的类型和所有的供应商  分页
    public Map<Object, Object> getAllSellerInfo(Integer moreId,PageParamDto pageParamDto){
    	Map<Object, Object> map = new LinkedHashMap<>();
    	//分页条件
    	SellerListParamDto sellerListParamDto = new SellerListParamDto();
    	if (moreId == 1) {
    		if(!StringUtils.isEmpty(pageParamDto)){
    			sellerListParamDto.setPageNo(pageParamDto.getPageNo());
    		}else{
    		   sellerListParamDto.setPageNo(1); // 更多查询所有的分类
    		}    		
    		sellerListParamDto.setPageSize(20);
		} else {
			sellerListParamDto.setPageNo(1);// 查询所有的分类
			sellerListParamDto.setPageSize(999);
		}
    	
    	sellerListParamDto.setSellerType("1"); //商户类型,定为工厂类型
    	sellerListParamDto.setAreaId(0);
    	sellerListParamDto.setCategoryId(0);
    	sellerListParamDto.setStageType("2");//pop平台
    	SellerListResultDto sellerListResultDto = sellerService.sellerList(sellerListParamDto).getData();
        List<SellerInfoVO> sellerVOList = sellerListResultDto.getSellerVOList();//获取所有的供应商列表

        List<SellerInfoVO> collect = sellerVOList.stream().filter(sellerInfoVO -> StringUtils.isEmpty(sellerInfoVO.getLogoUrl())).collect(Collectors.toList());
        sellerVOList.removeAll(collect);
        sellerVOList.addAll(collect);

        if(moreId!=1){
            Long total = sellerListResultDto.getTotal();
            map.put(new ProductResult<SellerCategoryInfo>( new SellerCategoryInfo("工厂"),(long)total), sellerVOList.subList(0,sellerVOList.size()>=10?10:sellerVOList.size()));
        }else{
        	Long total = sellerListResultDto.getTotal();
            map.put(new ProductResult<SellerCategoryInfo>( new SellerCategoryInfo("工厂"),(long)total), sellerVOList);
        }

    	return map;
    }
    
    //获取商家的详情和商家下的产品
    public Map<Object, Object> quoteFactoryDetail(Long sellerId){
    	//获取商家分类下的所有的商品
    	Map<Object, Object> map = new LinkedHashMap<>();
		GoodsSearchParamDto goodsSearchParamDto  = GoodsSearchParamDto.builder().sellerId(sellerId).build();
		GoodsCacheSearchResultDto data = categoryService.categoryGoodList(goodsSearchParamDto).getData();	
    	//获取商家的一级和三级分类
    	List<CategoryResultDto> CategoryResultDto = categoryService.getSellerAllCategorys(sellerId).getData();  	
    		for (CategoryResultDto category:CategoryResultDto) {
    			List<GoodsVO> list = New.arrayList(20);  
    			for (CategoryBaseResultDto category2:category.getCategoryList()) {
             	  for (CategoryBaseResultDto category3 : category2.getCategoryList()) {
             		  //将一级分类和商品对应起来
             		 if(!StringUtils.isEmpty(data)){
             			List<GoodsVO> goodsList = data.getProductSearchVo();
             			for (GoodsVO goodsVO : goodsList) {
             				if(goodsVO.getCategoryId().intValue() == category3.getCategoryId().intValue() ){
             					list.add(goodsVO);            					
             				}             				
             			}             			 
             		}            		       		  
  				}                       
              }
    			if(list.size() != 0){
    				map.put(category, list); 
    			}   			
    		} 		   		
    	return map;
    }

    /**
     *二级分类均匀获取首页展示
     */
    public Map<Object,Object> getAllGoodsCategoryList(){
        List<CategoryResultDto> categoryResultDtos = categoryService.listCategoryResult().getData();
        //得到所有的分类数据 取第一层分类 和 第三层分类
        Map<Object,Object> map = new LinkedHashMap<>(); //保存有序的一级分类集合

        for (CategoryResultDto category:categoryResultDtos) {
            List<CategoryBaseResultDto> list = New.arrayList(10);
            for (int i = 0,l = category.getCategoryList().size(); i < l; i++) {
                CategoryBaseResultDto dto = category.getCategoryList().get(i).getCategoryList().get(0);
                list.add(dto);
            }
            if(list.size()<10){
                for (int i = 0,l = category.getCategoryList().size(); i < l; i++) {
                    List<CategoryBaseResultDto> dtos = category.getCategoryList().get(i).getCategoryList().subList(1, category.getCategoryList().get(i).getCategoryList().size());
                    list.addAll(dtos);
                }
            }
            if(!category.getCategoryList().isEmpty()){
            map.put(category,list);
            }
        }
        return map;
    }

    //到分类更多页面 需要三级分类
    public Map<Object,Object> queryGoodscategory(){

        List<CategoryResultDto> categoryResultDtos = categoryService.listCategoryResult().getData();

        Map<Object,Object> map = new HashMap<>();

        List<CategoryBaseResultDto> levle1 = new ArrayList<>();
        List<CategoryBaseResultDto> levle2 = new ArrayList<>();
        List<CategoryBaseResultDto> levle3 = new ArrayList<>();

        //剔除没有三级分类的以及二级分类
        for (int i = categoryResultDtos.size()-1; i >=0 ; i--) {
            List<CategoryBaseResultDto> list = categoryResultDtos.get(i).getCategoryList();

            if(list.isEmpty()){
                categoryResultDtos.remove(categoryResultDtos.get(i));
            }else{
                for (int j = list.size()-1; j >=0 ; j--) {
                    if(list.get(j).getCategoryList().size()<1){
                        list.remove(list.get(j));
                        j--;
                        if(list.isEmpty()){
                            categoryResultDtos.remove(categoryResultDtos.get(i));
                            i--;
                        }
                    }
                }
                levle2.addAll(list);
            }
        }
        levle1.addAll(categoryResultDtos);
        levle3.addAll(categoryResultDtos.get(0).getCategoryList().get(0).getCategoryList());
        map.put(1,levle1);
        map.put(2,levle2);
        map.put(3,levle3);
        return map;
    }


    //获取当前分类下的三级分类
    public List<CategoryBaseResultDto> getCategoryLeavel3(Integer categoryId){

        List<CategoryBaseResultDto> level3 = new ArrayList<>();

        List<CategoryResultDto> categoryResultDtos = categoryService.listCategoryResult().getData();

        for (CategoryResultDto category:categoryResultDtos) {
            for(CategoryBaseResultDto category2 : category.getCategoryList()){
                if(category2.getCategoryId().intValue() == categoryId.intValue()){
                level3.addAll(category2.getCategoryList());
                }
            }
        }
        return level3;
    }


	// 根据分类id和分类类型查询 获取分类下的所有spu
	public GoodsResult<GoodsCacheSearchResultDto> queryGoodsByCategoryId(String categoryId) {
        GoodsSearchParamDto builder = GoodsSearchParamDto.builder().categoryId(categoryId).order(0).build();
        builder.setPageSize(20);
        return categoryService.categoryGoodList(builder);
    }

    //根据分类id到报价商品详情页面
    public QuoteProductInfo queryQuoteGoodsByCategoryId(Integer categoryId){
        return  quoteProductService.getProductInfo(categoryId);
    }
}
